import * as api from '../../services/Work/relationList';

export default {
  namespace: 'relationList',
  state: {
    loading: false,
    tabActive: "1",
    examine: {
      total: 0,
      page: 1,
      limit: 10,
      list: [],
    },
    done: {
      total: 0,
      page: 1,
      limit: 10,
      list: [],
    },
    examineList: [],
    doneList: [],
  },
  subscriptions: {
    setup() {},
  },
  effects: {
    *initialization({ payload }, { put, select }) {
      const orgCode = yield select(state => state.user.current_org.org_code);
      const tabActive = yield select(state => state.relationList.tabActive);
      yield put({ type: 'fetchList', payload: { orgCode, tabActive } });
    },
    *changeTabsActive({ payload: { key } }, { put, select }) {
      const orgCode = yield select(state => state.user.current_org.org_code);
      yield put({ type: 'saveTabActive', payload: key });
      yield put({ type: 'fetchList', payload: { orgCode, tabActive: key } });
    },
    *fetchList({ payload }, { put, call, select }) {
      const { tabActive, orgCode, page = 1, limit = 10 } = payload;

      yield put({ type: 'saveLoading', payload: false });

      let key; let status;
      switch ( tabActive) {
        case "1": {
          key = 'examine'; status = 0; break;
        }
        case "2": {
          key = 'done'; status = 1; break;
        }
        default: {
          console.warn(`[relaionList tabActive] value is abnormal!`); break;
        }
      }

      const currnetPage = yield select(state => state.relationList[key].page);
      if ( page !== currnetPage) {
        yield put({ type: 'savePage', payload: { key, page } });
      }
      const currnetLimit = yield select(state => state.relationList[key].limit);
      if ( limit !== currnetLimit) {
        yield put({ type: 'saveLimit', payload: { key, limit } });
      }

      const { data: { ret: data } } = yield call( api.getList, {
        origin_org_code: orgCode,
        goal_org_code: orgCode,
        page,
        limit,
        status,
      });

      yield put({ type: 'saveList', payload: { key, data } });
      yield put({ type: 'saveLoading', payload: true });
    },
  },
  reducers: {
    saveTabActive(state, action) {
      return {
        ...state,
        tabActive: action.payload,
      };
    },
    saveList(state, action) {
      const { key, data } = action.payload;
      const { total } = data;
      let { data: list } = data;
      const { page } = state[key];
      if (page > 1) {
        list = [
          ...state[key].list,
          ...list,
        ];
      }
      console.log(list);
      return {
        ...state,
        [key]: {
          ...state[key],
          total,
          list,
        },
      };
    },
    savePage(state, action) {
      const { key, page } = action.payload;
      return {
        ...state,
        [key]: {
          ...state[key],
          page,
        }
      };
    },
    saveLimit(state, action) {
      const { key, limit } = action.payload;
      return {
        ...state,
        [key]: {
          ...state[key],
          limit,
        }
      };
    },
    saveLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
